import { NextResponse } from 'next/server'; import { commerceDb } from '@/app/lib/commerce-db'; import { getCustomerUser } from '@/app/lib/lumen-auth'; export const runtime = 'nodejs'; export const dynamic = 'force-dynamic'; // Read-only summary of an UNPAID order, keyed by the order id. The id is // an unguessable capability token (lg_ + 10 chars from a 31-char // alphabet), so this mirrors the trust model of the resume-payment POST // (/api/orders/[id]/checkout): both run BEFORE the customer logs in, // because the magic link is deferred to the Stripe webhook. We expose // only pending_payment / abandoned orders so a leaked id can never // reveal a paid order's photos or details. // // Powers two screens that both arrive without the in-memory draft: // • the post-cancel / reminder resume screen (/checkout), which shows // the saved photos + design instead of a bare button, and // • the builder's "make changes" rehydration (/builder?edit=…), which // reconstructs the design + photos so nothing has to be re-uploaded. export async function GET( request: Request, { params }: { params: Promise<{ id: string }> }, ) { const { id } = await params; const admin = commerceDb(); if (new URL(request.url).searchParams.has('replace')) { const user = await getCustomerUser(); if (!user) return NextResponse.json({ error: 'Sign in to start a replacement.' }, { status: 401 }); const { data: saved } = await admin.from('orders').select('user_email, config, pet_photo_urls, custom_style_url') .eq('id', id).eq('user_id', user.id).eq('status', 'cancelled').maybeSingle(); if (!saved) return NextResponse.json({ error: 'Cancelled order not found.' }, { status: 404 }); const config = saved.config && typeof saved.config === 'object' && !Array.isArray(saved.config) ? saved.config : {}; const designFields = ['styleId', 'productId', 'size', 'orientation', 'frame', 'matteId', 'wrap', 'bgColorId', 'bgCustom', 'fontId', 'includeName', 'artistNote', 'pets']; const assetPath = `/api/orders/photos?order=${encodeURIComponent(id)}&slot=`; return NextResponse.json({ email: saved.user_email, config: Object.fromEntries(Object.entries(config).filter(([key]) => designFields.includes(key))), petPhotoUrls: (saved.pet_photo_urls ?? []).map((_: string, slot: number) => assetPath + slot), customStyleUrl: saved.custom_style_url ? assetPath + 'style' : null, }, { headers: { 'Cache-Control': 'private, no-store' } }); } const { data: order, error } = await admin .from('orders') .select('status, user_email, config, pet_photo_urls, custom_style_url, price_cents') .eq('id', id) .single(); if (error || !order) { return NextResponse.json({ error: 'order not found' }, { status: 404 }); } if (order.status !== 'pending_payment' && order.status !== 'abandoned') { return NextResponse.json({ error: 'order not resumable' }, { status: 409 }); } return NextResponse.json({ status: order.status, // Returned so the builder can prefill the email on the "make changes" // flow — the customer already gave it when the order was created, and // re-typing it to re-pay is friction. Same exposure class as the // pet photos this endpoint already returns, gated by the same // unguessable order-id capability token. email: order.user_email ?? '', config: order.config ?? {}, petPhotoUrls: order.pet_photo_urls ?? [], customStyleUrl: order.custom_style_url ?? null, priceCents: order.price_cents ?? 0, }); }